﻿#region File Description
//-----------------------------------------------------------------------------
// SupportTower.cs
//
// Attack of the Crazy Pixels
// Copyright (c) 2009 VosSoft
//-----------------------------------------------------------------------------
#endregion
#region License
//-----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2009 VosSoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Attack_of_the_Crazy_Pixels
{
    /// <summary>
    /// Defines a SupportTower.
    /// </summary>
    public sealed class SupportTower : Tower
    {
        #region Fields

        float rangeMultiplier, damageMultiplier, fireRateMultiplier, splashRangeMultiplier;
        List<WeaponTower> weaponTowers = new List<WeaponTower>();

        #endregion

        #region Properties

        public float RangeMultiplier
        {
            get { return rangeMultiplier; }
        }

        public float DamageMultiplier
        {
            get { return damageMultiplier; }
        }

        public float FireRateMultiplier
        {
            get { return fireRateMultiplier; }
        }

        public float SplashRangeMultiplier
        {
            get { return splashRangeMultiplier; }
        }

        public List<WeaponTower> WeaponTowers
        {
            get { return weaponTowers; }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Creates a new SupportTower.
        /// </summary>
        /// <param name="info">Reference to all Informations for this SupportTower.</param>
        /// <param name="position">Position of the SupportTower.</param>
        /// <param name="level">Backreference to the Level.</param>
        public SupportTower(SupportTowerInfo info, Vector2 position, Level level)
            : base(info, position, level)
        {
            damageMultiplier = info.DamageMultiplier[0];
            fireRateMultiplier = info.FireRateMultiplier[0];
            rangeMultiplier = info.RangeMultiplier[0];
            splashRangeMultiplier = info.SplashRangeMultiplier[0];

            // scan for WeaponTowers if we place a new SupportTower in the Level.
            if (level != null)
            {
                ScanRangeForWeaponTowers();
            }
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the SupportTower and apply all multipliers.
        /// </summary>
        /// <param name="elapsedTime">Time since the last frame (in seconds).</param>
        public override void Update(float elapsedTime)
        {
            foreach (WeaponTower tower in weaponTowers)
            {
                tower.Damage *= damageMultiplier;
                tower.FireRate *= fireRateMultiplier;
                tower.Range *= rangeMultiplier;
                tower.SplashRange *= splashRangeMultiplier;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Scans for all WeaponTowers in range and add them to the update list.
        /// </summary>
        public void ScanRangeForWeaponTowers()
        {
            weaponTowers.Clear();

            foreach (WeaponTower tower in level.Towers.OfType<WeaponTower>())
            {
                if ((Position - tower.Position).Length() <= Range)
                {
                    weaponTowers.Add(tower);
                }
            }
        }

        /// <summary>
        /// Adds a new WeaponTower to the SupportTowers update list.
        /// </summary>
        /// <param name="tower"></param>
        public void AddWeaponTower(WeaponTower tower)
        {
            weaponTowers.Add(tower);
        }

        /// <summary>
        /// Upgrades the SupportTower.
        /// </summary>
        public override void Upgrade()
        {
            if (upgradeLevel < Info.MaxUpgradeLevel)
            {
                base.Upgrade();

                range = Info.Range[upgradeLevel];
                rangeMultiplier = ((SupportTowerInfo)Info).RangeMultiplier[upgradeLevel];
                damageMultiplier = ((SupportTowerInfo)Info).DamageMultiplier[upgradeLevel];
                fireRateMultiplier = ((SupportTowerInfo)Info).FireRateMultiplier[upgradeLevel];
                splashRangeMultiplier = ((SupportTowerInfo)Info).SplashRangeMultiplier[upgradeLevel];

                ScanRangeForWeaponTowers();
            }
        }

        public override string ToString()
        {
            return base.ToString() + ", " + String.Format("RangeMultiplier = {0}, DamageMultiplier = {1}, FireRateMultiplier = {2}, SplashRangeMultiplier = {3}, WeaponTowersInRange = {4}",
                rangeMultiplier, damageMultiplier, fireRateMultiplier, splashRangeMultiplier, weaponTowers.Count);
        }

        #endregion
    }
}
